<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-table
        :data="roleList"
        border
        stripe
        style="width: 100%">
        <el-table-column
        type="expand">
        <template slot-scope="scope">
          <el-row :key="item.id" v-for="(item,i1) in scope.row.children" :class="['down-row',i1 === 0 ? 'top-row' : '']">
            <el-Col :span="4">
              <el-tag type="success" closable @close="closeTabHandle(scope.row, item.id)">{{item.authName}}</el-tag>
            </el-Col>
            <el-Col :span="20">
              <el-row :key="item1.id" v-for="(item1, i2) in item.children" :class="[i2 === 0 ? '' : 'top-row']">
                <el-Col :span="7">
                  <el-tag type="infp" closable @close="closeTabHandle(scope.row, item1.id)">{{item1.authName}}</el-tag>
                </el-Col>
                <el-col :span="17">
                  <el-tag type="warning" closable @close="closeTabHandle(scope.row, item2.id)" :key="item2.id" v-for="item2 in item1.children">{{item2.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-Col>
          </el-row>
        </template>
        </el-table-column>
        <el-table-column
        label="#"
        width="80"
        type="index">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称">
        </el-table-column>
        <el-table-column
          prop="roleDesc"
          label="角色描述">
        </el-table-column>
        <el-table-column
          prop="level"
          width="320"
          label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editRoleDialog(scope.row.id)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delRoleDialog(scope.row.id)">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="editRolePowerDialog(scope.row.id,'rightstree')">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="分配权限"
        :visible.sync="assignDialogVisible"
        width="50%"
        @close="closeDialog">
        <el-tree
        :data="rightTreeData"
        :default-expand-all="true"
        show-checkbox
        node-key="id"
        ref="rightsTree"
        :props="treeProp">
        </el-tree>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
export default {
  created () {
    this.getRoleList()
    this.getRightsTree()
  },
  data () {
    return {
      roleList: [],
      rightTreeData: [],
      assignDialogVisible: false,
      treeProp: {
        label: 'authName',
        children: 'children'
      },
      checkedKey: []
    }
  },
  methods: {
    async getRoleList () {
      const { data: res } = await this.$http.get('/roles')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.roleList = res.data
    },
    async closeTabHandle (role, rightId) {
      const { data: res } = await this.$http.delete(`/roles/${role.id}/rights/${rightId}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      role.children = res.data
    },
    editRolePowerDialog (roleId, ref) {
      this.assignDialogVisible = true
      var currRole = this.roleList.filter(item => item.id === roleId)
      this.getLeafKeys(currRole[0])
      // nextTick函数 会在DOM渲染完毕后回调这里用来解决无法获取tree的ref属性
      this.$nextTick(function () {
        this.$refs.rightsTree.setCheckedKeys(this.checkedKey)
        this.checkedKey.forEach(item => {
          this.$refs.rightsTree.store.nodesMap[item].expanded = true
          return true
        })
        console.log(this.$refs.rightsTree.store.nodesMap)
      })
      console.log('checkKey:' + this.checkedKey)
      console.dir(currRole)
    },
    // 递归获取默认勾选的数组
    getLeafKeys (role) {
      if (!role.children) {
        return this.checkedKey.push(role.id)
      }
      role.children.forEach(element => {
        this.getLeafKeys(element)
      })
    },
    closeDialog () {
      this.checkedKey = []
      this.expandedKey = []
    },
    async getRightsTree () {
      const { data: res } = await this.$http.get('/rights/tree')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.rightTreeData = res.data
      console.log(this.rightTreeData)
    }
  }
}
</script>
<style lang="less" scoped>
.el-row {
  display: flex;
  align-items: center;
}
.el-tag {
  margin: 8px;
}
.top-row {
  border-top: 1px solid #eee;
}
.down-row {
  border-bottom: 1px solid #eee;
}
</style>
